#include "Bed.h"
#include "Login.h"
#include "Siri.h"
#include "SliderDateTime.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_FromMyDialog.h"
#include <QMessageBox>
#include <QTime>
#include <QTimer>
#include <QVBoxLayout>
#include <QMap>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qDebug()<<"main thread:"<<QThread::currentThreadId()<<endl;
    mySqlite=new MySqlite();
    sqliteThread=new QThread;
    mySqlite->moveToThread(sqliteThread);
    sqliteThread->start();
  connect(&this->loginTimer,&QTimer::timeout,this,&MainWindow::loginOutTime);
  connect(&this->serverStatusTimer,&QTimer::timeout,this,&MainWindow::serverStatusTimerTimeout);
  longin=new Login();
  //connect(this->myTcpSock,&QTcpSocket::disconnected,[=](){qDebug()<<"......."<<endl;});
  longin->show();
  this->loginTimer.start(3*1000);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::loginOutTime()
{
    this->loginTimer.stop();
    if(this->mySqlite->connectSql())
    {
        connect(Login::myTCPClient,&MyTcp::disconnected_signal,this,&MainWindow::networkUnusual);
        //connect(Login::myTCPClient->getTask(),&Task::receiveServerData_signal,this,&MainWindow::receiveServerData_slot);
        connect(this,&MainWindow::sendServerData_signal,Login::myTCPClient,&MyTcp::sendServerData_slot);
        QMetaObject::invokeMethod(Login::myTCPClient,"connectServer",Qt::AutoConnection);
        QThread::sleep(3);//延时一会
//        if(Login::myTCPClient->getMyTcpSocket()->state()==QAbstractSocket::ConnectedState)
//                emit this->sendServerData_signal("7E7E;kind:surgery;function:01;",true);
//        else
//        {
//            QMessageBox* box=new QMessageBox;
//            box->warning(nullptr,"","连接服务器失败，请重新打开");
//            QApplication::quit();
//        }
        //emit this->sendServerData_signal("7E7E;kind:surgery;function:01;",true);
        emit this->sendServerData_signal("7E7E;kind:surgery;function:79;",false);
    }
    this->receiveServerData_slot(1,QMap<QString,QString>());
}

void MainWindow::networkUnusual()
{//没有收到服务器回来的信息
//        QMessageBox* box=new QMessageBox;
//        box->warning(nullptr,"","网络异常，请重新登陆");
//        QApplication::quit();
}

void MainWindow::receiveServerData_slot(int function, QMap<QString, QString> kv)
{
    switch (function) {
    case 1:
    {
        this->menuDialog=new FromMyDialog;
        QVBoxLayout *layout = new QVBoxLayout;
        this->menu_=new Menu;
        layout->addWidget(this->menu_);
        this->menuDialog->ui->downWidget->setLayout(layout);
        connect(this->menu_,&Menu::showSiri_signal,this->menuDialog,&FromMyDialog::showSiri_slot);
        connect(this->menu_,&Menu::showPatient_signal,this->menuDialog,&FromMyDialog::showPatient_slot);

        connect(this,&MainWindow::sendServerStatus,this->menuDialog,&FromMyDialog::showServerStatus);
        connect(this,&MainWindow::sendServerStatus,this->menu_->getBedDialog(),&FromMyDialog::showServerStatus);
        connect(this,&MainWindow::sendServerStatus,this->menu_->getsetDialog(),&FromMyDialog::showServerStatus);
        connect(this,&MainWindow::sendServerStatus,this->menu_->getLineDialog(),&FromMyDialog::showServerStatus);
        connect(this,&MainWindow::sendServerStatus,this->menu_->getOperDialog(),&FromMyDialog::showServerStatus);

        this->serverStatusTimer.start(this->serverStatusInterval);
        QMetaObject::invokeMethod(this,"serverStatusTimerTimeout",Qt::AutoConnection);
        this->menuDialog->show();
        delete this->longin;
    }
        break;

    }
}

void MainWindow::serverStatusTimerTimeout()
{
    emit sendServerStatus(Login::myTCPClient->getMyTcpSocket()->state()==QAbstractSocket::ConnectedState);
}

MySqlite *MainWindow::mySqlite=new MySqlite;
